﻿/******************************************************************************/
#include "stdafx.h"
#include "Main.h"
/******************************************************************************

   Here are listed various functions sending messages to the Server.

/******************************************************************************/
Connection Server ; // connection with the server
Str        Account; // my (clients) account name
NetChr     ChrData; // character data obtained from server when entering a game (on its base 'Game::Chr' for the player is created)
/******************************************************************************/
void Logout()
{
   ClientLogout(Server);
   Account.clear();
}
/******************************************************************************/
void Disconnect()
{
   ClientSendBye(Server);
}
/******************************************************************************/
Bool ServerRead()
{
   Neighbors.lock();

   // read messages from server
   for(;Server.receive(0);)
   {
      Byte   cmd=Server.data.getByte();
      switch(cmd)
      {
         case CS_CHR_DISCONNECT: {SockAddr   addr;           ClientRecvChrDisconnect(Server.data, addr      );                       Neighbors.removeKey(   addr)                               ;} break;
         case CS_CHR_CONNECT   : {ChrInfo    ci  ;           ClientRecvChrConnect   (Server.data, ci        );                      *Neighbors          (ci.addr)          =                  ci;} break;
         case CS_CHR_VISUALS   : {ChrVisuals cv  ;           ClientRecvChrVisuals   (Server.data, cv        ); if(Neighbor *neighbor=Neighbors.find     (cv.addr))*neighbor=                  cv;} break;
         case CS_CHR_POS       : {ChrPos     cp  ;           ClientRecvChrPos       (Server.data, cp        ); if(Neighbor *neighbor=Neighbors.find     (cp.addr))*neighbor=                  cp;} break;
         case CS_CHR_ANIM      : {SockAddr   addr; Str anim; ClientRecvChrAnim      (Server.data, addr, anim); if(Neighbor *neighbor=Neighbors.find     (   addr)) neighbor->playAnimation(anim);} break;
         case CS_CHAT          : {SockAddr   addr; Str text; ClientRecvChat         (Server.data, addr, text); if(Neighbor *neighbor=Neighbors.find     (   addr)) neighbor->said         (text);} break;
      }
   }

   Neighbors.unlock();

   return Server.state()==CONNECT_GREETED;
}
/******************************************************************************/
